CatTorneo = {};
CatTorneo.Version = '0.1';
CatTorneo.CompatibleWithJQuery = '1.5.1';
CatTorneo.Type = "jquery";

if (jQuery().jquery.indexOf(CatTorneo.CompatibleWithJQuery) != 0 && window.console && window.console.warn)
    console.warn("This version of CategoriaTorneo is tested with jQuery " + CatTorneo.CompatibleWithJQuery +
        " it may not work as expected with this version (" + jQuery().jquery + ")");
    
CatTorneo.each = jQuery.each;
CatTorneo.AjaxClass = {
    add: function(obj){
        jQuery(obj).addClass(SATK.Bases.ajaxClass);
    },
    remove: function(obj){
        jQuery(obj).removeClass(SATK.Bases.ajaxClass);
    }
};
    
CatTorneo.init = function(){
    CatTorneo.Categoria.initialize();
    CatTorneo.Cinta.initialize();
    CatTorneo.Peso.initialize();
};
// ---------------------- CatTorneo FORM -------
    
CatTorneo.Categoria = {
    isSelect: false,
    identificador: null, 
    nombre: null, 
    sigla: null,
    
    initialize: function(){
        this.identificador = jQuery("#nvaCategoria\\:iCategoria");
        this.nombre = jQuery("#nvaCategoria\\:nomcategoria");
        this.sigla = jQuery("#nvaCategoria\\:siglaCategoria");
        
        this.nombre.focusout(function(){
            CatTorneo.Categoria.closeAutocomplete(null, null);
        }).autocomplete({
            minLength: 1,
            source: function( request, response ) {
                CatTorneo.Categoria.sourceAjax(request, response);
            },
            select: function(event, ui) {
                CatTorneo.Categoria.selectAutocomplete(event, ui);
            },
            close:  function(event, ui) {
                CatTorneo.Categoria.closeAutocomplete(event, ui);
            },
            search: function(event, ui) {
                CatTorneo.AjaxClass.add(CatTorneo.Categoria.nombre);
                CatTorneo.Categoria.isSelect=false;
            },
            open: function(event, ui){
                CatTorneo.AjaxClass.remove(CatTorneo.Categoria.nombre);
            }
        });
    },
    
    sourceAjax: function(request, response){
        $.ajax({
            url: SATK.Bases.root +"/resources/json/autoCategoria.jsp", 
            dataType: "jsonp", 
            data: {
                sfind: request.term
            },
            success: function( data ) {
                response( $.map( data.categorias, function( item ) {
                    return {
                        label: item.name, 
                        value: item.name,
                        id: item.id, 
                        sigla:item.sigla
                    }
                })); // Termina (responce)
            } // Termina [success]
        }); // Termina $.AJAX
    },
    
    selectAutocomplete: function(event, ui){
        if(ui.item){
            this.identificador.attr("value", ui.item.id);
            this.sigla.attr({
                'value': ui.item.sigla, 
                'disabled': "disabled"
            });
            this.isSelect=true;
        }
    },
    
    closeAutocomplete: function(event, ui){
        if (!this.isSelect){
            this.identificador.attr("value", -1);
            this.sigla.attr({
                'value': "", 
                'disabled': ""
            });
        }
    }
    
};

CatTorneo.Cinta= {
    isSelect: false,
    identificador: null, 
    color: null,
    
    initialize: function(){
        this.identificador = jQuery("#nvaCategoria\\:iCinta");
        this.color = jQuery("#nvaCategoria\\:colorCinta");
        this.color.focusout(function(){
            CatTorneo.Cinta.closeAutocomplete(null, null);
        }).autocomplete({
            minLength: 1,
            source: function( request, response ) {
                CatTorneo.Cinta.sourceAjax(request, response);
            },
            select: function(event, ui) {
                CatTorneo.Cinta.selectAutocomplete(event, ui);
            },
            close:  function(event, ui) {
                CatTorneo.Cinta.closeAutocomplete(event, ui);
            },
            search: function(event, ui) {
                CatTorneo.AjaxClass.add(CatTorneo.Cinta.nombre);
                CatTorneo.Cinta.isSelect=false;
            },
            open: function(event, ui){
                CatTorneo.AjaxClass.remove(CatTorneo.Cinta.nombre);
            }
        });
    },
    
    sourceAjax: function(request, response){
        $.ajax({
            url: SATK.Bases.root+"/resources/json/autoCinta.jsp", 
            dataType: "jsonp", 
            data: {
                sfind: request.term
            },
            success: function( data ) {
                response( $.map( data.cintas, function( item ) {
                    return {
                        label: item.color, 
                        value: item.color, 
                        id: item.id, 
                        color: item.color
                    }
                })); // Termina (responce)
            } // Termina [success]
        }); // Termina $.AJAX
    },
    
    selectAutocomplete: function(event, ui){
        if(ui.item){
            this.identificador.attr("value", ui.item.id);
            this.isSelect=true;
        }
    },
    
    closeAutocomplete: function(event, ui){
        if (!this.isSelect){
            this.identificador.attr("value", -1);
        }
    }
};

CatTorneo.Peso = {
    input: null, 
    chk: null,
    initialize: function(){
        this.input = jQuery("#nvaCategoria\\:pesoKai");
        this.chk = jQuery("#chkModKae");
        this.input.autocomplete({
            minLength: 2,
            source: function( request, response ) {
                $.ajax({
                    url:SATK.Bases.root+"/resources/json/autoPeso.jsp", 
                    dataType:'jsonp', data: {sfind:extractLast(request.term)},
                    success: function(data){
                        response($.map(data.pesos, function(item){
                            return {id: item.id, label: item.peso, value:item.peso};
                        }));
                    }
                });
            },
            search: function() {
                CatTorneo.AjaxClass.add(CatTorneo.Peso.input);
                var term = extractLast(this.value);
                if(term.length<2)return false;
            },
            focus: function(){
                return false;
            },
            select: function(event, ui) {
                var terms = split(this.value);
                terms.pop();
                terms.push(ui.item.value);
                terms.push("");
                this.value = terms.join(", ");
                return false;
            },
            open: function(event, ui){
                CatTorneo.AjaxClass.remove(CatTorneo.Peso.input);
            }
        });
    }
}

jQuery(function(){
    CatTorneo.init();
});